package com.file.system.service.impl.business;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.file.system.entity.business.BusUtensil;
import com.file.system.entity.business.BusUtensilExcel;
import com.file.system.entity.business.BusUtensilIndentExcel;
import com.file.system.mapper.business.BusUtensilMapper;
import com.file.system.service.business.IBusUtensilService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;
import com.file.system.plugin.shiro.ShiroTools;
import com.file.system.tools.page.DP;
import com.file.system.entity.res.DataPage;
import cn.hutool.core.util.IdUtil;

import static com.file.system.tools.Formater.getTime;

import cn.hutool.core.util.ObjectUtil;

import java.time.LocalDateTime;


/**
 * 器具 实现
 *
 * @author jch
 * @since 2023-11-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusUtensilServiceImpl implements IBusUtensilService {

    private static final Logger logger = LogManager.getLogger(BusUtensilServiceImpl.class);

    @Autowired
    private BusUtensilMapper mapper;


    //添加器具
    public boolean save(BusUtensil busUtensil) {
        busUtensil.setId(IdUtil.getSnowflakeNextId());
        if (StrUtil.isEmpty(busUtensil.getDictWgsm())) busUtensil.setDictWgsm(null);
        if (StrUtil.isEmpty(busUtensil.getDictReportType())) busUtensil.setDictReportType(null);
        if (StrUtil.isEmpty(busUtensil.getDictTestDepartment())) busUtensil.setDictTestDepartment(null);
        if (StrUtil.isEmpty(busUtensil.getDictJzzq())) busUtensil.setDictJzzq(null);
        if (StrUtil.isEmpty(busUtensil.getDictJzpd())) busUtensil.setDictJzpd(null);
        return SqlHelper.retBool(mapper.save(busUtensil));
    }

    //添加器具(批量)
    public boolean saveBatch(@Param("list") List<BusUtensil> busUtensilList) {
        if (busUtensilList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(busUtensilList));
    }

    //添加或更新器具
    public boolean saveOrUpdate(BusUtensil busUtensil) {
        if (StrUtil.isEmpty(busUtensil.getDictWgsm())) busUtensil.setDictWgsm(null);
        if (StrUtil.isEmpty(busUtensil.getDictReportType())) busUtensil.setDictReportType(null);
        if (StrUtil.isEmpty(busUtensil.getDictTestDepartment())) busUtensil.setDictTestDepartment(null);
        if (StrUtil.isEmpty(busUtensil.getDictJzzq())) busUtensil.setDictJzzq(null);
        if (StrUtil.isEmpty(busUtensil.getDictJzpd())) busUtensil.setDictJzpd(null);
        return ObjectUtil.isNull(busUtensil.getId()) ? save(busUtensil) : updateById(busUtensil);
    }


    //编辑器具
    public boolean updateById(@Param("et") BusUtensil busUtensil) {
        logger.info("执行方法 -> updateById, 参数 -> busUtensil = {}", busUtensil);
        if (ObjectUtil.isEmpty(busUtensil.getId())) {
            return false;
        }
        if (StrUtil.isEmpty(busUtensil.getDictWgsm())) busUtensil.setDictWgsm(null);
        if (StrUtil.isEmpty(busUtensil.getDictReportType())) busUtensil.setDictReportType(null);
        if (StrUtil.isEmpty(busUtensil.getDictTestDepartment())) busUtensil.setDictTestDepartment(null);
        if (StrUtil.isEmpty(busUtensil.getDictJzzq())) busUtensil.setDictJzzq(null);
        if (StrUtil.isEmpty(busUtensil.getDictJzpd())) busUtensil.setDictJzpd(null);
        return SqlHelper.retBool(mapper.updateById(busUtensil));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<BusUtensil> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //删除器具
    public boolean removeById(@Param("id") Long id) {
        BusUtensil busUtensil = new BusUtensil();
        busUtensil.setId(id);
        return SqlHelper.retBool(mapper.removeById(busUtensil));
    }

    //通过ids删除器具
    public boolean removeByIds(List<Long> ids) {
        UpdateWrapper<BusUtensil> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_del", 1);
        updateWrapper.set("delete_by", ShiroTools.getId());
        updateWrapper.set("delete_name", ShiroTools.getName());
        updateWrapper.set("delete_time", LocalDateTime.now());
        updateWrapper.in("id", ids);

        int state = mapper.update(updateWrapper);
        if (state < 1) {
            return false;
        }
        return true;
    }

    //查询器具
    @DS("slave")
    public List<BusUtensil> list(@Param("ew") QueryWrapper<BusUtensil> query) {
        return mapper.select(query);
    }

    //通过ids查询器具集合
    @DS("slave")
    public List<BusUtensil> listByIds(List<Long> ids) {
        QueryWrapper<BusUtensil> query = new QueryWrapper<>();
        query.in("a.id", ids);
        query.orderByDesc("a.id");
        return mapper.select(query);
    }

    //查询所有非删除状态器具
    @DS("slave")
    public List<BusUtensil> listAll() {
        QueryWrapper<BusUtensil> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.orderByDesc("a.id");
        return mapper.select(query);
    }

    //查询器具分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<BusUtensil> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<BusUtensil> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<BusUtensil> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询器具
    @DS("slave")
    public BusUtensil getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询器具
    @DS("slave")
    public BusUtensil getByOne(@Param("ew") QueryWrapper<BusUtensil> query) {
        List<BusUtensil> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询器具数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<BusUtensil> query) {
        return mapper.selectCount(query);
    }

    //下载excel器具
    @DS("slave")
    public List<BusUtensilExcel> selectUtensilExcelList(long fkIndentId) {
        QueryWrapper<BusUtensil> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.eq("a.fk_indent_id", fkIndentId);
        query.orderByDesc("a.id");
        List<BusUtensil> utensilList = mapper.select(query);

        // 复制
        List<BusUtensilExcel> excelList = BeanUtil.copyToList(utensilList, BusUtensilExcel.class);
        return excelList;
    }

    // 查询器具信息
    public List<BusUtensilExcel> selectUtensilExcelListByIds(String ids) {
        QueryWrapper<BusUtensil> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        if (StrUtil.isNotEmpty(ids)) {
            query.in("a.fk_indent_id", ids.split(","));
        }
        query.orderByDesc("a.id");
        List<BusUtensil> utensilList = mapper.select(query);

        // 复制
        List<BusUtensilExcel> excelList = BeanUtil.copyToList(utensilList, BusUtensilExcel.class);
        return excelList;
    }
}
